A functor (“function object”) is a class that exists to encapsulate a method. (In many other languages, functions can be defined outside of classes, making functors unnecessary.)
The java.util.Comparator interface is one that is often implemented as a functor.
Use java.util.Collections.sort(List<T>, Comparator<? super T>) to sort objects. This is useful for objects that lack a compareTo method, or to sort them in a way other than what that method defines.
A Comparator’s compare(T o1, To2) method returns:
int ≤ -1o1 < o2.0o1 equals o2int ≥ 1o1 > o2
This ActionListener is basically a functor, implemented as an anonymous class.
quitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});